package weibo.tools;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;

public class loginTest {
	public static void main(String[] args) throws Exception {
//		HttpHost proxy = new HttpHost("localhost",8200);
		HttpClient httpClient = new DefaultHttpClient();
		
//		httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY,proxy);
		
		HttpPost httpUriRequest = new HttpPost("http://login.weibo.cn/login/?rand=1255945006&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%E6%96%B0%E6%B5%AA%E5%BE%AE%E5%8D%9A&vt=4&revalid=2&ns=1");
		httpUriRequest.addHeader("Content-Type", "application/x-www-form-urlencoded");
		httpUriRequest.addHeader("User-Agent","Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36");
		httpUriRequest.addHeader("Accept", "*/*");	
//		httpUriRequest.addHeader("Host","3g.sina.com.cn");
		httpUriRequest.addHeader("Referer","http://3g.sina.com.cn/prog/wapsite/sso/register_submit.php?type=m&rand=1491089280&backURL=http%3A%2F%2Fweibo.cn%2F&backTitle=%D0%C2%C0%CB%CE%A2%B2%A9&vt=3");
		httpUriRequest.addHeader("Accept-Language","en-US,en;q=0.8");

		List <NameValuePair> nvps = new ArrayList <NameValuePair>();
		
		nvps.add(new BasicNameValuePair("mobile", "Marilyn.Willson@sneakypeaky.ml"));
		nvps.add(new BasicNameValuePair("password_2748", "Nike123"));
		nvps.add(new BasicNameValuePair("vk", "2748_8465_1650446940"));
		nvps.add(new BasicNameValuePair("backURL", "http%3A%2F%2Fweibo.cn%2F"));
		String a = "新浪微博";
		String u8 = new String(a.getBytes("UTF-8"));
		nvps.add(new BasicNameValuePair("backTitle",a ));
		nvps.add(new BasicNameValuePair("type", "m"));
		nvps.add(new BasicNameValuePair("capId", "2_ead9e94e63006e76"));
		
		httpUriRequest.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));

		HttpResponse httpResponse = null;
		try {
			httpResponse = httpClient.execute(httpUriRequest);
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		HttpEntity entity = httpResponse.getEntity();
		String html = null;
		try {
			html = new String(EntityUtils.toString(entity).getBytes());
			
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        File file = new File("/Users/henrylian/Desktop/Test1.html");
        FileWriter fwriter = new FileWriter(file);
        fwriter.write(html);
        fwriter.close();
	}

}
